//
//  WZHomeTableViewCell.swift
//  weibo
//
//  Created by mac on 16/3/1.
//  Copyright © 2016年 mac. All rights reserved.
/*

*/

import UIKit
import SnapKit

class WZHomeTableViewCell: UITableViewCell {
    
    var footerConstraints: Constraint?
//   MARK:cellViewModel的didSet方法 在为cellViewModel赋值的时候将cellViewModel传递给原创微博的view
    var cellViewModel: WZCellViewModel?{
        didSet{
            originView.cellViewModel = cellViewModel
            footerView.cellViewModel = cellViewModel
            
            
            footerConstraints?.uninstall()
            if cellViewModel?.model?.retweeted_status == nil {//如果转发微博是空的说明不是转发微博这样就要把转发微博view隐藏，并且修改footerConstraints

                retweetView.hidden = true
                
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    footerConstraints = make.top.equalTo(originView.snp_bottom).constraint
                })
                
            }else{
                retweetView.hidden = false
                retweetView.cellViewModel = cellViewModel
                
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                footerConstraints = make.top.equalTo(retweetView.snp_bottom).constraint
                    
                })
            }
            
            
            
        }
    }
    
//    MARK:cell的初始化方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //    MARK:原创微博view的懒加载
    lazy var originView: WZOriginView = WZOriginView()
    //    MARK:底部view的懒加载
    lazy var footerView: WZFooterView = WZFooterView()
    
    //    MARK:转发微博内容的懒加载
    lazy var retweetView:WZretweetView = WZretweetView()
    
   
    
    //    MARK:将子view设置到contentView上并为子空间添加约束
    private func setupUI(){
//        MARK:注意在这如果给cell设置的约束自动获取行高就会失效
        contentView.addSubview(originView)
        
        contentView.addSubview(footerView)
        
        contentView.addSubview(retweetView)
        
//        设置原创微博的约束
        originView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
//            make.bottom.equalTo(contentView.snp_bottom)
        }
        
//        设置转发微博内容的的约束
        retweetView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left).offset(10)
            make.right.equalTo(contentView.snp_right).offset(-10)
            
            make.top.equalTo(originView.snp_bottom)
            
           
        }

        //        设置底部按钮的约束
        footerView.snp_makeConstraints { (make) -> Void in
            
            self.footerConstraints = make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.left.equalTo(contentView.snp_left).offset(10)
            
            
            make.right.equalTo(contentView.snp_right).offset(-10)
            make.bottom.equalTo(contentView.snp_bottom)
            make.height.equalTo(40)

        }
        
//       设置contenView的约束
//        contentView.snp_makeConstraints { (make) -> Void in
//            make.left.equalTo(self.snp_left)
//            make.top.equalTo(self.snp_top)
//            make.right.equalTo(self.snp_right)
//            make.bottom.equalTo(self.snp_bottom)
//        }
       
        
    }
}

